package org.itboys.mobile.service.mysql.web;


import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.dto.lasted.SundayUploadDto;

import org.itboys.admin.entity.lasted.SundayUpload;
import org.itboys.admin.entity.lasted.SystemDictionary;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.admin.service.lasted.SystemDictionaryService;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mysql.SundayProduct;
import org.itboys.mobile.entity.mysql.SundayProductCategory;
import org.itboys.mobile.service.mongo.web.SundayActivityRelationService;
import org.itboys.mobile.service.mongo.web.SundayMemberService;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;


/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_商品_service_web
 * 当前为单规格。如果需要多规格，请自行扩展
 */
@Service
public class SundayProductService extends BaseMySqlService<SundayProduct> {
	@Autowired
	private SundayUploadService uploadService;
	@Autowired
	private SundayProductCategoryService categoryService;
	@Autowired
	private SystemDictionaryService dictionaryService;
	@Autowired
	private SundayActivityRelationService relationService;
	/**
	 * 新增偏方
	 * @param product
	 * @param rollImageFiles
	 * @throws IOException
	 */
	@Transactional
	public void save(
			SundayProduct product,
			MultipartFile defaultImageFile,
			MultipartFile backImageFile,
			MultipartFile[] rollImageFiles
	) throws  Exception {

		Map<String, String> backMap = uploadService.uploadSingle(backImageFile);
		if(StringUtils.isNotEmpty(backMap.get(uploadService.UPLOAD_HALF_PATH))){
			product.setBackImage(backMap.get(uploadService.UPLOAD_HALF_PATH));
		}
		Map<String, String> defaultMap = uploadService.uploadSingle(defaultImageFile);
		if(StringUtils.isNotEmpty(defaultMap.get(uploadService.UPLOAD_HALF_PATH))){
			product.setDefaultImage(defaultMap.get(uploadService.UPLOAD_HALF_PATH));
		}
		//编辑
		if(LongUtil.isNotZreo(product.getId())){
			super.update(product);
		}else{
			super.insert(product);
		}


		//保存轮播图
		uploadService.uploadMultiWithRecord(rollImageFiles,product.getId(),SundayProduct.class.getSimpleName(),"商品ID"+product.getId()+",的轮播图");

	}

	/**
	 * 分页查询商品
	 * @param request
	 * @return
	 */
	public PageResult<SundayProduct> selectProduct(HttpServletRequest request) {
		Map<String, Object> param = ParamUtil.packageMysqlPageParam(request, "name","status","categoryId","isIndexRecommend");
		return new PageResult<SundayProduct>(super.select(param), super.count(param));
	}


	/**
	 * 查询商品详情。后台
	 * @param productId
	 * @return
	 */
	public SundayProduct findOne(Long productId){
		SundayProduct product=null;
		//组装标签
		List<SystemDictionary> dictionaries=dictionaryService.getDictionaryByKey("house_tag");
		if(LongUtil.isNotZreo(productId)){
			product = super.find(productId);
			if(product.getTag()==null)product.setTag("");
			String [] tagIds = product.getTag().split(",");
			List<Map<String,Object>> tags = new ArrayList<Map<String, Object>>();
			for(SystemDictionary dictionary:dictionaries){
				Map<String,Object> tag=new HashMap<String,Object>();
				tag.put("id",dictionary.getId());
				tag.put("name",dictionary.getValue());
				int isChecked=0;
				for(String tagId:tagIds){
					if(tagId.equalsIgnoreCase(String.valueOf(dictionary.getId()))){
						isChecked=1;
						break;
					}
				}
				tag.put("isChecked",isChecked);
				tags.add(tag);
			}
			//轮播图
			List<SundayUploadDto> rollImages = uploadService.selectNoPage(product.getId(),SundayProduct.class.getSimpleName());
			product.setRollImages(rollImages);
			product.setTags(tags);
		}else {
			product=new SundayProduct();
			if(product.getTag()==null)product.setTag("");
			String [] tagIds = product.getTag().split(",");
			List<Map<String,Object>> tags = new ArrayList<Map<String, Object>>();
			for(SystemDictionary dictionary:dictionaries){
				Map<String,Object> tag=new HashMap<String,Object>();
				tag.put("id",dictionary.getId());
				tag.put("name",dictionary.getValue());
				int isChecked=0;
				for(String tagId:tagIds){
					if(tagId.equalsIgnoreCase(String.valueOf(dictionary.getId()))){
						isChecked=1;
						break;
					}
				}
				tag.put("isChecked",isChecked);
				tags.add(tag);
			}
			product.setTags(tags);
		}

		return product;
	}

	public void set(Long id,Integer type,Integer status){
		SundayProduct product=super.find(id);
		if(type==1){
			product.setIsRecommend(status);
		}
		if(type==2){
			product.setIsHot(status);
		}
		super.update(product);
	}

	public void delete(Long id){
		super.delete(id);
		//同时删除活动相关的楼盘
		Map<String,Object> param=new HashMap<String,Object>();
		param.put("isDeleted",SundayCommonConstants.NO);
		param.put("otherId",id);
		param.put("type",1);
		relationService.delete(param);
	}

}

